﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Markup;
using System.Text.RegularExpressions;

namespace Fireball.Syntax.Parser
{

    public class PatternFindResult
    {
        public PatternFindResult()
        {
            GC.KeepAlive(this);
        }
        public static readonly PatternFindResult Empty = new PatternFindResult();

        public int StartPos { get; set; }
        public int EndPos { get; set; }
        public string Text { get; set; }

        public Pattern Pattern { get; set; }

        //public override int GetHashCode()
        //{
        //    return base.GetHashCode();
        //}

        //public override bool Equals(object obj)
        //{
        //    PatterFindResult other = (PatterFindResult)obj;

        //    if (this.EndPos == other.EndPos)
        //        if (this.StartPos == other.StartPos)
        //            if (this.Text == other.Text)
        //                return true;
        //    return false;
        //}
    }

    [ContentProperty("Text")]
    public class Pattern 
    {
        private Regex _ComplexRegex = null;

        public virtual void Prepare()
        {
            if (_ComplexRegex == null)
            {
                _ComplexRegex = new Regex(this.Text, RegexOptions.Compiled);
            }
        }


        public virtual PatternFindResult FindIn(string line, int startPos)
        {
            if (this.IsComplex && this._ComplexRegex == null)
                this.Prepare();

            if (this.IsComplex)
            {

                var m = _ComplexRegex.Match(line, startPos);

                if (m.Success)
                {
                    return new PatternFindResult
                    {
                        StartPos = m.Index,
                        EndPos = m.Index + m.Length,
                        Text = m.Value,
                        Pattern = this
                    };
                }
                else

                    return PatternFindResult.Empty;
            }
            else
            {
                if ((startPos + this.Text.Length) > line.Length)
                    return null;

                var g = line.Substring(startPos, this.Text.Length);

                if (g == this.Text)
                {
                    if (!((startPos + this.Text.Length) == line.Length))
                    {
                        var endChar = line.Substring(startPos + this.Text.Length, 1);

                        if (endChar != " ") //Controllare che non sia un bracket
                            return null;
                    }

                    return new PatternFindResult()
                    {
                        EndPos = startPos + this.Text.Length,
                        StartPos = startPos,
                        Text = g,
                        Pattern = this
                    };
                }
                else
                    return null;
            }
            /*int i = line.IndexOf(this.Text, startPos);
            if (i == -1)
                return PatterFindResult.Empty;
            return new PatterFindResult
            {
                 StartPos= i,
                 EndPos = i + this.Text.Length,
                 Text = this.Text
            };*/
        }
        #region Properties

        public bool IsComplex { get; set; }



        public string Name
        {
            get;
            set;
        }

        public string Text
        {
            get;
            set;
        }


        public PatternGroup PatternGroup
        {
            get;
            set;
        }

        #endregion
    }
}
